package com.cjq.dao.Impl;

import com.cjq.bean.Courier;
import com.cjq.dao.BaseCourierDao;
import com.cjq.util.DBUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourierDaoMysql extends DBUtils implements BaseCourierDao {
    //用于查询数据库中的全部快递快递员（总数+新增）
    public static final String sql_courier_console = "select count(id) data1_size,count(TO_DAYS(createtime)=TO_DAYS(NOW()) or null) data1_day from courier";

    public static final String sql_courier_findAll = "select * from courier";
    public static final String sql_courier_findAll_limit = "select * from courier limit ?,?";
    public static final String sql_courier_zongExpress = "select count(id) jianshu from express where status = 1 and sysphone = ?";
    public static final String sql_courier_insert = "insert into courier (sysname,sysphone,cardid,password,createtime) values(?,?,?,?,NOW())";
    public static final String sql_courier_findBySysPhone = "select * from courier where sysphone = ?";
    public static final String sql_courier_update = "update courier set sysname = ?,sysphone = ?,cardid = ?,password = ? where id =?";
    public static final String sql_courier_updateExpressSysPhone = "update express set sysphone = ? where sysphone = ?";
    public static final String sql_courier_delete = "delete from courier where id = ?";
    public static final String sql_courier_updateLoginTime = "update courier set logintime=NOW(), loginip=?  where sysphone = ? ";
    public static final String sql_courier_updateJianShu = "update courier set jianshu= ? where sysphone = ? ";

    @Override
    public List<Map<String, Integer>> console() {
        List<Map<String, Integer>> data = new ArrayList<>();
        try {
            ResultSet rs = query(sql_courier_console, null);
            if (rs.next()) {
                int data1_size = rs.getInt("data1_size");
                int data1_day = rs.getInt("data1_day");
                Map data1 = new HashMap();

                data1.put("data1_size", data1_size);

                data1.put("data1_day", data1_day);

                data.add(data1);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeall();
        }

        return data;
    }

    @Override
    public List<Courier> findAll(boolean limit, int offset, int pageNumber) {
        List<Courier> data = new ArrayList<>();
        ResultSet rs = null;

        try {
            if (limit) {   //limit为true表示为true   表示分页查询
                List params = new ArrayList();
                params.add(offset);
                params.add(pageNumber);
                rs = query(sql_courier_findAll_limit, params);
            } else {
                rs = query(sql_courier_findAll, null);
            }


            ResultSetMetaData metaData = rs.getMetaData();     //存储结果集信息
            int columnCount = metaData.getColumnCount(); //得到列数
            String[] columnNames = new String[columnCount];  //存放结果的列名
            for (int i = 0; i < columnCount; i++) {
                columnNames[i] = metaData.getColumnName(i + 1);
            }

            //得到实体类中的所有方法  获取bean中的Student类的所有方法
            Method[] methods = Courier.class.getDeclaredMethods();
            while (rs.next()) {

                Courier courier = new Courier();
                for (String columnname : columnNames) {
                    String name = "set" + columnname;
                    for (Method method : methods) {
                        if (method.getName().equalsIgnoreCase(name)) {

                            method.invoke(courier, rs.getObject(columnname));//执行了对应的set方法
                            break;
                        }
                    }
                }
                data.add(courier);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return data;
    }


    @Override
    public int zongExpress(String sysPhone) {
        int jianshu = 0;
        try {
            List params = new ArrayList();
            params.add(sysPhone);
            ResultSet rs = query(sql_courier_zongExpress, params);
            if (rs.next()) {
                jianshu = rs.getInt("jianshu");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeall();
        }
        return jianshu;
    }

    @Override    //sql_courier_insert
    public boolean insert(Courier c) {

        try {
            List params = new ArrayList();
            params.add(c.getSysName());
            params.add(c.getSysPhone());
            params.add(c.getCardId());
            params.add(c.getPassword());


            int update = update(sql_courier_insert, params);
            System.out.println(update + "ddddddd");

            if (update > 0) {
                System.out.println("vvvv");
                return true;
            } else {
                System.out.println("bbbbb");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return false;
    }

    @Override
    public Courier findBySysPhone(String sysPhone) {
        Courier courier = null;
        try {
            List params = new ArrayList();
            params.add(sysPhone);
            ResultSet rs = query(sql_courier_findBySysPhone, params);


            ResultSetMetaData metaData = rs.getMetaData();     //存储结果集信息
            int columnCount = metaData.getColumnCount(); //得到列数
            String[] columnNames = new String[columnCount];  //存放结果的列名
            for (int i = 0; i < columnCount; i++) {
                columnNames[i] = metaData.getColumnName(i + 1);
            }

            //得到实体类中的所有方法  获取bean中的Student类的所有方法
            Method[] methods = Courier.class.getDeclaredMethods();
            while (rs.next()) {
                courier = new Courier();
                for (String columnname : columnNames) {
                    String name = "set" + columnname;
                    for (Method method : methods) {
                        if (method.getName().equalsIgnoreCase(name)) {
                            method.invoke(courier, rs.getObject(columnname));//执行了对应的set方法
                            break;
                        }
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return courier;

    }

    @Override
    public boolean update(Courier c) {

        List params = new ArrayList();
        try {
            params.add(c.getSysName());
            params.add(c.getSysPhone());
            params.add(c.getCardId());
            params.add(c.getPassword());
            params.add(c.getId());
            return  update(sql_courier_update, params)>0?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return false;

    }


    @Override
    public boolean updateExpressSysPhone(String sysPhone, String g_sysPhone) {
        List params = new ArrayList();
        try {
            System.out.println(sysPhone);
            System.out.println(g_sysPhone);
            params.add(sysPhone);
            params.add(g_sysPhone);

            int update = update(sql_courier_updateExpressSysPhone, params);


            if (update > 0) {

                return true;
            } else {

                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return false;
    }

    @Override
    public boolean delete(int id) {
        List params = new ArrayList();
        try {
            params.add(id);
            int update = update(sql_courier_delete, params);
            if (update>0){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return false;

    }


    @Override
    public boolean updateLoginTime(String phone,String ip) {
        try {
            List params = new ArrayList();
            System.out.println(ip);
            System.out.println(phone);
            params.add(ip);
            params.add(phone);
            return update(sql_courier_updateLoginTime,params)>0?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return false;
    }


    @Override
    public boolean updateJianShuBySysPhone(int jianShu, String sysPhone) {
        try {
            List params = new ArrayList();
            params.add(jianShu);
            params.add(sysPhone);
            return update(sql_courier_updateJianShu,params)>0?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return false;
    }
}
